package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.vo.ConcernStats;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.DateUtil;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Service
public class UserInfoCacheServiceImpl implements IUserInfoCacheService {

    @Autowired
    private StringRedisTemplate template;
    @Override
    public void setUserInfo(String token, String value) {
        String cacheKey = RedisKey.USER_INFO.getCacheKey(token);
        template.opsForValue().set(cacheKey,value,RedisKey.USER_INFO.getTimeout(), TimeUnit.MILLISECONDS);
    }

    @Override
    public String getUserInfo(String token) {
        String cacheKey = RedisKey.USER_INFO.getCacheKey(token);
        String value = template.opsForValue().get(cacheKey);
        //重置有效时间
        if (value != null){
            template.expire(cacheKey,RedisKey.USER_INFO.getTimeout(), TimeUnit.MILLISECONDS);
        }
        return value;
    }

    @Override
    public AjaxResult addConcern(ConcernStats concern, Long userId) {
        AjaxResult result = new AjaxResult();
        //判断id是否不一致
        if (concern.getUserId() == userId){
            return result.mark("关注者是自己!");
        }
        //判断用户id是否为空
        if (userId == null){
            return result.mark("关注失败");
        }

        List<ConcernStats> concerns = new ArrayList<>();
        //获取缓存key
        String cacheKey = RedisKey.USER_INFO.getCacheKey(userId);
        //判断是否是第一次关注人
        if (template.hasKey(cacheKey)){
            //判断是否已经关注
            String vo = template.opsForValue().get(cacheKey);
            concerns = JSON.parseArray(vo, ConcernStats.class);
            if (concerns.contains(concern)){//已经关注过
                return result.mark("已经关注过了");
            }
            //未关注
            concerns.add(concern);

        }else{//第一次关注人
            concerns.add(concern);
        }
        template.opsForValue().set(cacheKey,JSON.toJSONString(concerns));
        //设置粉丝数据
        setFens(concern.getUserId(),userId);
        result.setMsg("关注成功");
        return result;
    }


    @Override
    public List<ConcernStats> getConcerns(Long userId) {
        //获取缓存key
        String cacheKey = RedisKey.USER_INFO.getCacheKey(userId);
        String vo = template.opsForValue().get(cacheKey);
        List<ConcernStats> concerns = JSON.parseArray(vo, ConcernStats.class);
        return concerns;
    }


    @Override
    public void setFens(Long userId, Long fensId) {
        //获取key
        String cacheKey = RedisKey.USER_FENS.getCacheKey(userId);
        List<Long> fens = new ArrayList<>();
        //判断是否第一个粉丝
        if (template.hasKey(cacheKey)){
            String vo = template.opsForValue().get(cacheKey);
            fens = JSON.parseArray(vo,Long.class);
            fens.add(fensId);
        }else{
            fens.add(fensId);
        }

        template.opsForValue().set(cacheKey,JSON.toJSONString(fens));
    }


    @Override
    public int getFensNum(Long userId) {
        String cacheKey = RedisKey.USER_FENS.getCacheKey(userId);
        String vo = template.opsForValue().get(cacheKey);
        List<Long> fens = JSON.parseArray(vo,Long.class);
        return fens == null ? 0:fens.size();
    }

    @Override
    public void setVisitors(Long userId, Long visitorId) {
        //获取用户今日访客的key
        String cacheKey = RedisKey.USER_TOTAY_VISITORS.getCacheKey(userId);
        List<Long> todayVisitorS = new ArrayList<>();
        //判断今天有没有被人访问过
        if(template.hasKey(cacheKey)){
            String vo = template.opsForValue().get(cacheKey);
            todayVisitorS = JSON.parseArray(vo,Long.class);
            //判断用户是否已经被访问过
            if (todayVisitorS.contains(visitorId)){
                //已经访问
                return;
            }
            //未访问
            todayVisitorS.add(visitorId);

        }else {//没有人访问
            todayVisitorS.add(visitorId);
        }
        //保存今日访客
        Date now = new Date();
        Date endDate = DateUtil.getEndDate(now);
        //往redis中存入key
        template.opsForValue().set(cacheKey,JSON.toJSONString(todayVisitorS), DateUtil.getDateBetween(now,endDate), TimeUnit.MILLISECONDS);
        //累计访客数+1
        String cacheKey1 = RedisKey.USER_ALL_VISITOR.getCacheKey(userId);
        //判断有没有人访问过
        if (template.hasKey(cacheKey1)){
            String vo = template.opsForValue().get(cacheKey1);
            Integer num = Integer.valueOf(vo) + 1;
            template.opsForValue().set(cacheKey1,num.toString());
        }else {
            //美人访问过
            template.opsForValue().set(cacheKey1,"1");
        }
    }

    @Override
    public int getTodayVisitorNum(Long userId) {
        String cacheKey = RedisKey.USER_TOTAY_VISITORS.getCacheKey(userId);
        String vo = template.opsForValue().get(cacheKey);
        List<Long> list = JSON.parseArray(vo, Long.class);
        return list == null? 0 : list.size();
    }

    @Override
    public int getAllVisitorNum(Long userId) {
        String cacheKey = RedisKey.USER_ALL_VISITOR.getCacheKey(userId);
        String vo = template.opsForValue().get(cacheKey);
        return vo == null? 0 :Integer.valueOf(vo);
    }
}
